<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 17-2-22
 * Time: 下午5:05
 */

$anonymous = function ($input){
    echo $input.PHP_EOL;
};

$anonymous(__FILE__);
$anonymous(dirname(__FILE__));
$anonymous(fileowner(__FILE__));

$anonymous(__DIR__);


$uses = __FILE__;
//闭包可以从父作用域中继承变量。 任何此类变量都应该用 use 语言结构传递进去。
$closures = function ($arg) use ($uses){
    echo $arg . $uses . PHP_EOL;
};

$closures('welcome  ');


call_user_func(function ($arg){
    echo __FUNCTION__ . $arg;
},time());


$value = 0;
$fn = function &() use (&$value) { return $value; };

$x =& $fn();
var_dump($x, $value);        // 'int(0)', 'int(0)'
++$x;
var_dump($x, $value);        // 'int(1)', 'int(1)'
++$x;
var_dump($x, $value);        // 'int(2)', 'int(2)'



class MyClass {
    const member = 1;

    public $member;

    public function member () {
        return "method 'member'";
    }

    public function __construct () {
        $this->member = function () {
            return "anonymous function 'member'";
        };

    }
}

$myObj = new MyClass();

var_dump(MyClass::member);  // int(1)
var_dump($myObj->member);   // object(Closure)#2 (0) {}
var_dump($myObj->member()); // string(15) "method 'member'"
$myMember = $myObj->member;
var_dump($myMember());      // string(27) "anonymous function 'member'"


const CON = "CONST";
$con = constant("CON");
echo $con;

$arr = [1,2,3,4];
array_walk($arr,function (&$value){
    $value *= $value;
});

print_r($arr);